//
//  ICMCarInfoEditView.m
//  ICrEAM
//
//  Created by xphone on 16/11/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMCarInfoEditView.h"
#import "NSDate+convenience.h"
#import "BrandSeriesResponse.h"


@implementation ICMCarInfoEditView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        self.backgroundColor = BackgroundColor_White;

        [self addSubview:self.carVINView];
        UIButton *syncVehicleInfoBtn=[[UIButton alloc]initWithFrame:CGRectMake(ActualWidth(190), ActualHeight(0), ActualWidth(25), ActualHeight(25))];
//        syncVehicleInfoBtn.backgroundColor=[UIColor redColor];
//        [syncVehicleInfoBtn setTitle:@"同步" forState:UIControlStateNormal];
      NSString *image=[ICrEAMTool isVW]?@"蓝色同步":@"getcar_synchronize_sk";
        [syncVehicleInfoBtn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
       
        [syncVehicleInfoBtn addTarget:self action:@selector(syncVehicleInfoClick) forControlEvents:UIControlEventTouchUpInside];
        [self.carVINView addSubview:syncVehicleInfoBtn];
        [self addSubview:self.carBrandView];
        [self addSubview:self.carNumView];
        [self addSubview:self.carInvoiceView];
        [self addSubview:self.carInvoiceBtn];
        
        [self addSubview:self.carXiView];
        [self addSubview:self.carTypeView];
        [self addSubview:self.carUsePropertyView];
        [self addSubview:self.carPropertyView];
        
        [self addSubview:self.carMyComBuyCarView];
        [self addSubview:self.carUseTypeView];
        
        [self addSubview:self.carGuaranteeView];
        [self addSubview:self.carGuaranteeBtn];
        [self addSubview:self.carKMView];
        [self addSubview:self.carNextTestView];
        [self addSubview:self.carNextTestBtn];
        
        [self getCarBrandData];
        
    }
    return self;
}
-(void)syncVehicleInfoClick{
    if ([self.delegate respondsToSelector:@selector(syncVehicleInfoWithVin:)]) {
        [self.delegate syncVehicleInfoWithVin:self.carVINView.contentText];
    }
}
//获取品牌车系车型信息
- (void)getCarBrandData{
    self.carBrandDataArray = [ICrEAMTool queryBrandSeries];
    NSMutableArray *brandNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *brandNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.carBrandDataArray.count;i++)
    {
        BrandModel *brandData = self.carBrandDataArray[i];
        [brandNameArray addObject:brandData.BRAND_NAME];
        [brandNoArray addObject:brandData.BRAND_CODE];
    }
    self.carBrandView.clickDataArray = brandNameArray;
    self.carBrandView.clickDataNoArray = brandNoArray;
}


//VIN
- (ICMTitleTextFeildView *)carVINView
{
    if(!_carVINView)
    {
        _carVINView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(20), ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"VIN"];
        _carVINView.isUppercase = YES;
    }
    return _carVINView;
}

//品牌
- (ICMTitleTextFeildView *)carBrandView
{
    if(!_carBrandView)
    {
        _carBrandView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.carVINView.rightEdge + ActualWidth(20), ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"品牌" addTarget:nil selector:nil];
        _carBrandView.delegate = self;
        
    }
    return _carBrandView;
}

//车牌号
- (ICMTitleTextFeildView *)carNumView
{
    if(!_carNumView)
    {
        _carNumView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(ScreenWidth/2 + ActualWidth(20), ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"车牌号"];
        _carNumView.isUppercase = YES;
    }
    return _carNumView;
}

//购车发票日期
- (ICMTitleTextFeildView *)carInvoiceView
{
    if(!_carInvoiceView)
    {
        _carInvoiceView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.carNumView.rightEdge + ActualWidth(20), ActualHeight(25), ActualWidth(182), ActualHeight(63)) title:@"购车发票日期" addTarget:self selector:@selector(clickCalenderBtnFrom)];
    }
    return _carInvoiceView;
}

- (UIButton *)carInvoiceBtn
{
    if(!_carInvoiceBtn)
    {
        _carInvoiceBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.carInvoiceView.rightEdge+ActualWidth(2), self.carInvoiceView.bottomEdge -ActualHeight(36), ActualWidth(36), ActualWidth(36))];
        [_carInvoiceBtn addTarget:self action:@selector(clickCalenderBtnFrom) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ActualHeight(30), ActualWidth(30))];
        imageView.image = [UIImage imageNamed:Appointment_Calendar];
        [_carInvoiceBtn addSubview:imageView];
    }
    return _carInvoiceBtn;
}

- (void)clickCalenderBtnFrom{
    [Window addSubview:self.carInvoiceDatePickerView];
}


- (PACACalendarView *)carInvoiceDatePickerView{
    if(!_carInvoiceDatePickerView){
        _carInvoiceDatePickerView = [PACACalendarView createCalendarViewWithDidSelectedDate:nil minDate:nil maxDate:nil andFrame:CGRectMake(ScreenWidth/2-150, ScreenHeight/2-150, ActualWidth(300), ActualWidth(380))];
        _carInvoiceDatePickerView.delegate = self;
        
    }
    return _carInvoiceDatePickerView;
}

#pragma mark ICMDatePickerViewDelegate
/*ICMDatePickerViewDelegate*/
- (void)calendarView:(PACACalendarView *)calendarView didSelectedDate:(NSDate*)selectedDate{
    if(calendarView == self.carInvoiceDatePickerView){
        self.carInvoiceSelectedDate = selectedDate;
        NSString *dateString = [selectedDate separatorStyleDateTimeString];
        self.carInvoiceView.contentTextFeild.text = dateString;
    }
    if(calendarView == self.carGuaranteeDatePickerView){
        self.carGuaranteeSelectedDate = selectedDate;
        NSString *dateString = [selectedDate separatorStyleDateTimeString];
        self.carGuaranteeView.contentTextFeild.text = dateString;
    }
    if(calendarView== self.carNextTestDatePickerView){
        self.carNextTestSelectedDate = selectedDate;
        NSString *dateString = [selectedDate separatorStyleDateTimeString];
        self.carNextTestView.contentTextFeild.text = dateString;
    }
}

//清空时间
- (void)delTime:(PACACalendarView *)calendarView
{
    if(calendarView == self.carInvoiceDatePickerView){
        self.carInvoiceSelectedDate = nil;
        self.carInvoiceView.contentTextFeild.text = @"";
    }
    if(calendarView == self.carGuaranteeDatePickerView){
        self.carGuaranteeSelectedDate = nil;
        self.carGuaranteeView.contentTextFeild.text = @"";
    }
    if(calendarView == self.carNextTestDatePickerView){
        self.carNextTestSelectedDate = nil;
        self.carNextTestView.contentTextFeild.text = @"";
    }
}


//车系
- (ICMTitleTextFeildView *)carXiView
{
    if(!_carXiView)
    {
        _carXiView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(20), self.carVINView.bottomEdge + ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"车系" addTarget:nil selector:nil];
        _carXiView.delegate = self;
        
    }
    return _carXiView;
}

//车型
- (ICMTitleTextFeildView *)carTypeView
{
    if(!_carTypeView)
    {
        _carTypeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.carXiView.rightEdge + ActualWidth(20), self.carVINView.bottomEdge + ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"车型" addTarget:nil selector:nil];
        _carTypeView.delegate = self;
        
    }
    return _carTypeView;
}

//使用性质
- (ICMTitleTextFeildView *)carUsePropertyView
{
    if(!_carUsePropertyView)
    {
        _carUsePropertyView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(ScreenWidth/2 + ActualWidth(20), self.carVINView.bottomEdge + ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"使用性质" addTarget:nil selector:nil];
        NSArray *cleanCarDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumUseNaturePre)];
        _carUsePropertyView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:cleanCarDataArray] mutableCopy];
        
    }
    return _carUsePropertyView;
}

//车辆性质
- (ICMTitleTextFeildView *)carPropertyView
{
    if(!_carPropertyView)
    {
        _carPropertyView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.carUsePropertyView.rightEdge + ActualWidth(20), self.carVINView.bottomEdge + ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"车辆性质" addTarget:nil selector:nil];
        NSArray *cleanCarDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumCarNaturePre)];
        _carPropertyView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:cleanCarDataArray] mutableCopy];
        
    }
    return _carPropertyView;
}

//本公司购车
- (ICMTitleTextFeildView *)carMyComBuyCarView
{
    if(!_carMyComBuyCarView)
    {
        _carMyComBuyCarView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(20), self.carXiView.bottomEdge + ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"本公司购车" addTarget:nil selector:nil];
        NSArray *cleanCarDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumYESOrNOPre)];
        _carMyComBuyCarView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:cleanCarDataArray] mutableCopy];
        
    }
    return _carMyComBuyCarView;
}

//车辆用途
- (ICMTitleTextFeildView *)carUseTypeView
{
    if(!_carUseTypeView)
    {
        _carUseTypeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(ScreenWidth/2 + ActualWidth(20), self.carXiView.bottomEdge + ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"车辆用途" addTarget:nil selector:nil];
        NSArray *cleanCarDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumCarUsesPre)];
        _carUseTypeView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:cleanCarDataArray] mutableCopy];
        
    }
    return _carUseTypeView;
}

//保修结束日期
- (ICMTitleTextFeildView *)carGuaranteeView
{
    if(!_carGuaranteeView)
    {
        _carGuaranteeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(20), self.carMyComBuyCarView.bottomEdge + ActualHeight(25), ActualWidth(182), ActualHeight(63)) title:@"保修结束日期" addTarget:self selector:@selector(clickCalenderBtnFrom2)];
    }
    return _carGuaranteeView;
}

- (UIButton *)carGuaranteeBtn
{
    if(!_carGuaranteeBtn)
    {
        _carGuaranteeBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.carGuaranteeView.rightEdge+ActualWidth(2), self.carGuaranteeView.bottomEdge -ActualHeight(36), ActualWidth(36), ActualWidth(36))];
        [_carGuaranteeBtn addTarget:self action:@selector(clickCalenderBtnFrom2) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ActualHeight(30), ActualWidth(30))];
        imageView.image = [UIImage imageNamed:Appointment_Calendar];
        [_carGuaranteeBtn addSubview:imageView];
    }
    return _carGuaranteeBtn;
}

- (void)clickCalenderBtnFrom2{
    [Window addSubview:self.carGuaranteeDatePickerView];
}

- (PACACalendarView*)carGuaranteeDatePickerView{
    if(!_carGuaranteeDatePickerView){
        _carGuaranteeDatePickerView = [PACACalendarView createCalendarViewWithDidSelectedDate:nil minDate:nil maxDate:nil andFrame:CGRectMake(ScreenWidth/2-150, ScreenHeight/2-150, ActualWidth(300), ActualWidth(380))];
        _carGuaranteeDatePickerView.delegate = self;
        
    }
    return _carGuaranteeDatePickerView;
}

//保修结束里程
- (ICMTitleTextFeildView *)carKMView
{
    if(!_carKMView)
    {
        _carKMView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(self.carGuaranteeBtn.rightEdge+ActualWidth(20), self.carMyComBuyCarView.bottomEdge + ActualHeight(25), ActualWidth(220), ActualHeight(63)) title:@"保修结束里程"];
        _carKMView.contentTextFeild.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _carKMView;
}

//下次验车日期
- (ICMTitleTextFeildView *)carNextTestView
{
    if(!_carNextTestView)
    {
        _carNextTestView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(ScreenWidth/2 + ActualWidth(20), self.carMyComBuyCarView.bottomEdge + ActualHeight(25), ActualWidth(182), ActualHeight(63)) title:@"下次验车日期" addTarget:self selector:@selector(clickCalenderBtnFrom3)];
    }
    return _carNextTestView;
}

- (UIButton *)carNextTestBtn
{
    if(!_carNextTestBtn)
    {
        _carNextTestBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.carNextTestView.rightEdge+ActualWidth(2), self.carNextTestView.bottomEdge -ActualHeight(36), ActualWidth(36), ActualWidth(36))];
        [_carNextTestBtn addTarget:self action:@selector(clickCalenderBtnFrom3) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ActualHeight(30), ActualWidth(30))];
        imageView.image = [UIImage imageNamed:Appointment_Calendar];
        [_carNextTestBtn addSubview:imageView];
    }
    return _carNextTestBtn;
}

- (void)clickCalenderBtnFrom3{
    [Window addSubview:self.carNextTestDatePickerView];
}

- (PACACalendarView *)carNextTestDatePickerView{
    if(!_carNextTestDatePickerView){
        _carNextTestDatePickerView  = [PACACalendarView createCalendarViewWithDidSelectedDate:nil minDate:nil maxDate:nil andFrame:CGRectMake(ScreenWidth/2-150, ScreenHeight/2-150, ActualWidth(300), ActualWidth(380))];
        _carNextTestDatePickerView.delegate = self;
        
    }
    return _carNextTestDatePickerView;
}

#pragma mark 赋值
- (void)setupData
{
    self.carVINView.contentText = self.carModel.vinNum;
    self.carBrandView.contentText = self.carModel.brandName;
    self.carBrandView.noStr = self.carModel.brand;
    self.carNumView.contentText = self.carModel.carNum;
    if(!self.carModel.parchaseDate || [self.carModel.parchaseDate isEqualToString:@""] || [self.carModel.parchaseDate isEqualToString:@"0"])
    {
        self.carInvoiceView.contentText = @"";
    }
    else
    {
        self.carInvoiceView.contentText = [ICrEAMTool getDateWithTimeInterval:self.carModel.parchaseDate];
    }
    
    self.carXiView.contentText = self.carModel.carKeiName;
    self.carXiView.noStr = self.carModel.carKei;
    self.carTypeView.contentText = self.carModel.carTypeName;
    self.carTypeView.noStr = self.carModel.carType;
    self.carUsePropertyView.contentText = [ICrEAMTool getCodeModelForCode:self.carModel.useNature].desc;
    self.carPropertyView.contentText = [ICrEAMTool getCodeModelForCode:self.carModel.carNature].desc;
    
    self.carMyComBuyCarView.contentText = [self.carModel.isOurBranch isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)]?@"是":@"否";
    self.carUseTypeView.contentText = [ICrEAMTool getCodeModelForCode:self.carModel.carUses].desc;
    
    if(!self.carModel.guaranteeEndDate || [self.carModel.guaranteeEndDate isEqualToString:@""] || [self.carModel.guaranteeEndDate isEqualToString:@"0"])
    {
        self.carGuaranteeView.contentText = @"";
    }
    else
    {
        self.carGuaranteeView.contentText = [ICrEAMTool getDateWithTimeInterval:self.carModel.guaranteeEndDate];
    }
    
    self.carKMView.contentText = self.carModel.guaranteeEndMillions;
    
    if(!self.carModel.nextCheckDate || [self.carModel.nextCheckDate isEqualToString:@""] || [self.carModel.nextCheckDate isEqualToString:@"0"])
    {
        self.carNextTestView.contentText = @"";
    }
    else
    {
        self.carNextTestView.contentText = [ICrEAMTool getDateWithTimeInterval:self.carModel.nextCheckDate];
    }
    //初始化SeriesList 和 ModelList
    [self setupSeriesListWithBrandCode:self.carModel.brand];
    [self setupModelListWithBrandCode:self.carModel.brand WithSeriesCode:self.carModel.carKei];
}

- (NSArray *)carBrandDataArray
{
    if(!_carBrandDataArray)
    {
        _carBrandDataArray = [[NSArray alloc] init];
    }
    return _carBrandDataArray;
}

#pragma mark ICMTitleTextFeildViewDelegate
- (void)clickedData:(ICMTitleTextFeildView *)titleTextFeildView
{
    if(titleTextFeildView == self.carBrandView)
    {
        self.carXiView.contentText = @"";
        self.carXiView.noStr = @"";
        self.carTypeView.contentText = @"";
        self.carTypeView.noStr = @"";
        [self setupSeriesListWithBrandCode:self.carBrandView.noStr];
    }
    if(titleTextFeildView == self.carXiView)
    {
        self.carTypeView.contentText = @"";
        self.carTypeView.noStr = @"";
        [self setupModelListWithBrandCode:self.carBrandView.noStr WithSeriesCode:self.carXiView.noStr];
        
    }
}

#pragma mark 根据品牌code查找车系list
- (void)setupSeriesListWithBrandCode:(NSString *)code
{
    NSMutableArray *seriesNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *seriesNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.carBrandDataArray.count;i++)
    {
        BrandModel *brandData = self.carBrandDataArray[i];
        if([brandData.BRAND_CODE isEqualToString:code])
        {
            NSArray *tempSeriesArray = brandData.TM_SERIES;
            for(int j = 0;j<tempSeriesArray.count;j++)
            {
                SeriesModel *serieddata = tempSeriesArray[j];
                [seriesNameArray addObject:serieddata.SERIES_NAME];
                [seriesNoArray addObject:serieddata.SERIES_CODE];
            }
        }
        
    }
    self.carXiView.clickDataArray = seriesNameArray;
    self.carXiView.clickDataNoArray = seriesNoArray;
}

#pragma mark 根据车系code查找车型list
- (void)setupModelListWithBrandCode:(NSString *)bcode WithSeriesCode:(NSString *)scode
{
    NSMutableArray *modelNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *modelNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.carBrandDataArray.count;i++)
    {
        BrandModel *brandData = self.carBrandDataArray[i];
        if([brandData.BRAND_CODE isEqualToString:bcode])
        {
            NSArray *tempSeriesArray = brandData.TM_SERIES;
            for(int j = 0;j<tempSeriesArray.count;j++)
            {
                SeriesModel *serieddata = tempSeriesArray[j];
                if([serieddata.SERIES_CODE isEqualToString:scode])
                {
                    NSArray *tempModelArray = serieddata.TM_MODEL;
                    for(int z = 0;z<tempModelArray.count;z++)
                    {
                        TMModel *modelData = tempModelArray[z];
                        [modelNameArray addObject:modelData.MODEL_NAME];
                        [modelNoArray addObject:modelData.MODEL_CODE];
                    }
                }
                
                
            }
        }
        
    }
    self.carTypeView.clickDataArray = modelNameArray;
    self.carTypeView.clickDataNoArray = modelNoArray;
}

@end
